package com.zsh.weather.ln.home.ui.care

import android.os.Handler
import android.os.Message
import android.view.View
import android.widget.ImageView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.zsh.weather.ln.R
import com.zsh.weather.ln.base.BaseMVPFragment
import com.zsh.weather.ln.bean.CityEntry
import com.zsh.weather.ln.db.AppDatabase
import com.zsh.weather.ln.home.adapter.CareAdapter
import com.zsh.weather.ln.home.ui.care.contract.CareContract
import com.zsh.weather.ln.home.ui.care.presenter.CardPresenter
import com.zsh.weather.ln.utils.Utils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
class CareFragment : BaseMVPFragment<CareContract.View, CardPresenter>(), CareContract.View {
    private lateinit var rv_view: RecyclerView
    private lateinit var adapter: CareAdapter
    private lateinit var iv_no: ImageView
    override fun initView(v: View) {
        iv_no = v.findViewById(R.id.iv_no)
        rv_view = v.findViewById(R.id.rv_view)
        rv_view.layoutManager = LinearLayoutManager(mContext)
    }

    override fun onResume() {
        super.onResume()
        presenter.getCitys()
    }

    var mHandler = object : Handler() {
        override fun handleMessage(msg: Message) {
            when (msg.what) {
                1 -> {
                    var list = msg.obj as MutableList<CityEntry>
                    if(list.size > 0){
                        rv_view.visibility = View.VISIBLE
                        iv_no.visibility = View.GONE
                    }
                    adapter = CareAdapter(list)
                    rv_view.adapter = adapter
                    click()
                }
            }
        }
    }

    private fun click() {
        adapter.addChildClickViewIds(R.id.iv_delete)
        adapter.setOnItemClickListener(object : OnItemClickListener {
            override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
                var cityEntry = adapter.data.get(position) as CityEntry
                Utils.openUrl(mContext, cityEntry.fxLink, "")
            }
        })
        adapter.setOnItemChildClickListener(object : OnItemChildClickListener{
            override fun onItemChildClick(
                adapter: BaseQuickAdapter<*, *>,
                view: View,
                position: Int
            ) {
            when(view.id){
                R.id.iv_delete ->{
                    GlobalScope.launch(Dispatchers.IO){
                        var entry = adapter.data.get(position) as CityEntry
                        adapter.data.removeAt(position)
                        AppDatabase.getDatabase(mContext).cityDao().deleteCitys(entry)
                    }
                    adapter.notifyDataSetChanged()
                }
            }
            }
        })
    }
    override fun getCitys(citys: MutableList<CityEntry>) {
        var msg = Message.obtain()
        msg.obj = citys
        msg.what = 1
        mHandler.sendMessage(msg)
    }

    override fun createPresenter(): CardPresenter {
        return CardPresenter()
    }

    override fun error(msg: String) {
    }

    override fun hideLoading() {
    }

    override fun showLoading() {
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_care_city
    }


}